package com.yunkaweilai.android.view.popup;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.interfaces.PopupEmployeeInterfaces;
import com.yunkaweilai.android.model.member.PercentagesStaffModel;
import com.yunkaweilai.android.model.operation.SelectedEmpModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import razerdp.basepopup.BasePopupWindow;

/**
 * 新增会员提成人
 * Created by Administrator on 2017/7/17.
 */

public class ShopEmployeePopup3 extends BasePopupWindow {

    private View popupView;
    private Activity mContext;
    private ListView idListView;
    private TextView idTvType;
    private CommonAdapter<PercentagesStaffModel.DataBean.ListBean> adapter;
    private ArrayList<PercentagesStaffModel.DataBean.ListBean> list = new ArrayList<>();
    private TextView idTvRight;
    private String employeeStr;
    private Map<String, SelectedEmpModel> selMap = new HashMap<>();
    private PopupEmployeeInterfaces popupEmployeeInterfaces;
    private String url;
    private String levelid;

    public ShopEmployeePopup3(Activity context, String employeeStr, String url, String levelid) {
        super(context);
        this.mContext = context;
        this.employeeStr = employeeStr;
        this.levelid = levelid;
        this.url = url;
        bindEvent();
    }

    public void setInterface(PopupEmployeeInterfaces popupEmployeeInterfaces) {
        this.popupEmployeeInterfaces = popupEmployeeInterfaces;
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.id_img_close);
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_shop_employee, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.id_rlayout_bottom);
    }

    private void bindEvent() {
        if (!StringUtils.isEmpty(employeeStr)) {
            initStr();
        }
        if (popupView != null) {
            initView();
            initAdapter();
            initHttp();
            initClick();
        }
    }

    private void initClick() {
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String empStr = "";
                String empName = "";
                float allScale = 0;
                for (String key : selMap.keySet()) {
                    SelectedEmpModel selectedEmpModel = selMap.get(key);
                    empStr = empStr + selectedEmpModel.getId() + "/" + selectedEmpModel.getScale() + "-";
                    allScale = allScale + Float.parseFloat(selectedEmpModel.getScale());
                }
                if (empStr.length() > 0) {
                    empStr = empStr.substring(0, empStr.length() - 1);
                }
                if (selMap.size() == 0) {
                    MyToast.showShortToast(mContext, "还未选中提成员工");
                } else if (selMap.size() == 1) {
                    for (String key : selMap.keySet()) {
                        SelectedEmpModel selectedEmpModel = selMap.get(key);
                        empName = empName + selectedEmpModel.getName();

                    }
                } else {
                    for (String key : selMap.keySet()) {
                        SelectedEmpModel selectedEmpModel = selMap.get(key);
                        empName = empName + selectedEmpModel.getName() + ",";
//                        break;
                    }
                }
                if (empName.endsWith(",")) {
                    empName = empName.substring(0, empName.length() - 1);
                }
                popupEmployeeInterfaces.refresh(empName, empStr);
                dismiss();

            }
        });
    }

    private void initStr() {
        int index = employeeStr.indexOf("-");
        String id;
        while (index > 0) {
            String cutSte = employeeStr.substring(0, index);
            SelectedEmpModel smodel = new SelectedEmpModel();
            int cutIndex = cutSte.indexOf("/");
            id = cutSte.substring(0, cutIndex);
            smodel.setId(id);
            String status = cutSte.substring(cutIndex + 1, cutSte.length());
            int staIndex = status.indexOf("/");
            String substring = status.substring(staIndex + 1, status.length());
            smodel.setScale(substring);
            employeeStr = employeeStr.substring(index + 1);
            index = employeeStr.indexOf("-");
            selMap.put(id, smodel);
        }
        SelectedEmpModel smodel = new SelectedEmpModel();
        int cutIndex = employeeStr.indexOf("/");
        id = employeeStr.substring(0, cutIndex);
        smodel.setId(id);
        String status = employeeStr.substring(cutIndex + 1, employeeStr.length());
        int staIndex = status.indexOf("/");
        String substring = status.substring(staIndex + 1, status.length());
        smodel.setScale(substring);
        selMap.put(id, smodel);
    }

    private void initHttp() {
        HttpRequest.postUrl(url)
                .addParams("level_id", levelid)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            PercentagesStaffModel percentagesStaffModel = gson.fromJson(json, PercentagesStaffModel.class);
                            list.clear();
                            list.addAll(percentagesStaffModel.getData().getList());
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });

    }

    private boolean ischange = true;

    private void initAdapter() {
        adapter = new CommonAdapter<PercentagesStaffModel.DataBean.ListBean>(mContext, R.layout.item_list_shop_employee2, list) {
            @Override
            protected void convert(ViewHolder viewHolder, final PercentagesStaffModel.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.id_check_name, "  "+item.getEmployee_name());
                View pointView = viewHolder.getView(R.id.id_tv_point);
                final EditText idEdtScale = viewHolder.getView(R.id.id_edt_scale);
                CheckBox idCheckName = viewHolder.getView(R.id.id_check_name);
                final SelectedEmpModel smodel = selMap.get(item.getId());
                if (smodel != null) {
                    idCheckName.setChecked(true);
                    idEdtScale.setVisibility(View.VISIBLE);
                    ischange = true;
                    idEdtScale.setText(smodel.getScale());
                    ischange = false;
                    pointView.setVisibility(View.VISIBLE);
                    smodel.setName(item.getEmployee_name());

                } else {
                    idEdtScale.setVisibility(View.INVISIBLE);
                    pointView.setVisibility(View.INVISIBLE);
                    idCheckName.setChecked(false);
                }
                idCheckName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (smodel != null) {
                            selMap.remove(item.getId());
                        } else {
                            SelectedEmpModel ceshi = new SelectedEmpModel();
                            ceshi.setId(item.getId());
                            ceshi.setName(item.getEmployee_name());
                            ceshi.setScale("100");
                            selMap.put(item.getId(), ceshi);
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
                idEdtScale.setTag(item.getId());
                idEdtScale.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (!ischange) {
                            if (item.getId().equals(idEdtScale.getTag())) {
                                SelectedEmpModel selectedEmpModel = selMap.get(idEdtScale.getTag());
                                if (selectedEmpModel != null) {
                                    selectedEmpModel.setScale(idEdtScale.getText().toString() + "");
                                }
                            }
                        }
                    }
                });

            }
        };
        idListView.setAdapter(adapter);
    }

    public static final String TAG = "asd";

    private void initView() {
        idListView = ((ListView) popupView.findViewById(R.id.id_listView));
        idTvRight = ((TextView) popupView.findViewById(R.id.id_tv_right));
        idTvType = ((TextView) popupView.findViewById(R.id.id_tv_type));
        idTvType.setText("选择提成员工");
    }
}
